import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:juju/conf/IMConf.dart';
import 'package:juju/default/Kits.dart';
import 'package:juju/fake/fake_message_tool.dart';
import 'package:juju/model/UserInfoModel.dart';

class MyInfoPage extends StatefulWidget {
  const MyInfoPage({Key? key}) : super(key: key);

  @override
  State<MyInfoPage> createState() => _MyInfoPageState();
}

class _MyInfoPageState extends State<MyInfoPage> {
  UserInfo? me = UserInfoModel.getInstance().myInfo;

  File? _image;
  final picker = ImagePicker();

  @override
  void initState() {
    super.initState();
    print("======================= my info ======================");
    print("|\n| ----- ${me!.nickname}\n");
    print("|\n| ----- ${me!.username}\n");
    print("|\n| ----- ${me!.avatarThumbPath}\n");
  }

  @override
  Widget build(BuildContext context) {
    // print(me.avatarThumbPath);
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.only(top: 40.0, bottom: 14.0),
            child: ListTile(
              leading: GestureDetector(
                child: Container(
                  clipBehavior: Clip.hardEdge,
                  // padding: EdgeInsets.all(5.0),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(15.0),
                  ),
                  child: avatar(me!.avatarThumbPath),
                ),
                onTap: () {
                  _getImage();
                },
              ),
              title: Text(
                  (me!.nickname == "null" || me!.nickname == "") ? me!.username : me!.nickname),
              subtitle: Text("聚聚号：${me!.username}"),
              trailing: IconButton(
                  icon: const Icon(Icons.edit), onPressed: () => _gotoModifyPage()),
            ),
          ),
          const Divider(
            thickness: 2.0,
          ),
          // RaisedButton(
          //   onPressed: getImage,
          //   child: Text("或个图片"),
          // ),
          Padding(
            padding: const EdgeInsets.only(top: 14.0, bottom: 14.0),
            child: ListTile(
              title: const Text("设置"),
              trailing: const Icon(Icons.chevron_right),
              onTap: () => Navigator.of(context).pushNamed("SettingPage"),
            ),
          ),
        ],
      ),
    );
  }

  void _getImage() async {
    await picker.pickImage(source: ImageSource.gallery).then((pickedFile) {
      if (pickedFile != null) {
        setState(() {
          _image = File(pickedFile.path);
        });
        _setAvatar();
      } else {
        print(
            "=============================== pickedFile is null ============================");
      }
    });
  }

  void _setAvatar() async {
    if (_image != null) {
      await IMConf.getInstance()
          .getJmessage()
          .updateMyAvatar(imgPath: _image!.path)
          .then(
        (value) {
          IMConf.getInstance()
              .getJmessage()
              .downloadThumbUserAvatar(username: me!.username)
              .then((value) {
            setState(() {
              me!.avatarThumbPath = value['filePath'];
            });
          }, onError: (e) {
            print(e);
          });
        },
        onError: (e) {
          print(e);
        },
      );
    } else {
      print(
          "=========================== _image is null ==========================");
    }
  }

  void _gotoModifyPage() {}
}
